package com.beyondsoft.sabg.edu.controller;

import com.beyondsoft.sabg.edu.model.db.DocumentContent;
import com.beyondsoft.sabg.edu.model.vo.core.SimpleResponseVO;
import com.beyondsoft.sabg.edu.service.AiMiaoBiService;
import com.beyondsoft.sabg.edu.service.DocumentContentService;
import com.beyondsoft.sabg.edu.utils.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * 公文处理控制器
 */
@RestController
@RequestMapping("/api/document")
@Slf4j
public class DocumentController {



    @Autowired
    private DocumentContentService documentContentService;

    @Autowired
    private AiMiaoBiService aiMiaoBiService;


    /**
     * 保存公文
     */
    @PostMapping("/save")
    public SimpleResponseVO saveDocument(@RequestBody DocumentContent document) {
        try {
            DocumentContent saved = documentContentService.save(new DocumentContent(),document);
            return ResponseUtil.success().withData(saved);
        } catch (Exception e) {
            log.error("保存公文失败", e);
            return ResponseUtil.failed("保存公文失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取公文
     */
    @GetMapping("/{id}")
    public SimpleResponseVO getDocument(@PathVariable Long id) {
        try {
            Optional<DocumentContent> documentContentOptional = documentContentService.findById(id);
            if (documentContentOptional.isPresent() ) {
                return ResponseUtil.success().withData(documentContentOptional.get());
            } else {
                return ResponseUtil.failed("公文不存在");
            }
        } catch (Exception e) {
            log.error("获取公文失败", e);
            return ResponseUtil.failed("获取公文失败：" + e.getMessage());
        }
    }

    /**
     * 根据公文类型查询公文列表
     */
    @GetMapping("/list")
    public SimpleResponseVO getDocumentList(@RequestParam(required = false) String documentType,
                                          @RequestParam(required = false) String title) {
        try {
            List<DocumentContent> documents;
            
            if (documentType != null && !documentType.trim().isEmpty()) {
                documents = documentContentService.findByDocumentType(documentType);
            } else if (title != null && !title.trim().isEmpty()) {
                documents = documentContentService.findByTitleContaining(title);
            } else {
                documents = documentContentService.findAll();
            }
            
            return ResponseUtil.success().withData(documents);
        } catch (Exception e) {
            log.error("查询公文列表失败", e);
            return ResponseUtil.failed("查询公文列表失败：" + e.getMessage());
        }
    }

    /**
     * 删除公文
     */
    @DeleteMapping("/{id}")
    public SimpleResponseVO deleteDocument(@PathVariable Long id) {
        try {
            documentContentService.deleteByIds(Collections.singletonList(id));
            return ResponseUtil.success().withMessage("删除成功");
        } catch (Exception e) {
            log.error("删除公文失败", e);
            return ResponseUtil.failed("删除公文失败：" + e.getMessage());
        }
    }
} 